<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>角色设置</title>
    <jsp:include page="easyuiIncludeJS.jsp"/>
    <script type="text/javascript">
        $(function () {
            var pager = $(".easyui-datagrid").datagrid('getPager');
            $(pager).pagination({
                total:${pager.totalRows},
                pageSize:${pager.pageRow},
                displayMsg: '{from}-{to}/{total}',
                pageNumber:${pager.cur_page},
                afterPageText: '页，共{pages}页',
                pageList: [5, 10, 20, 30, 50, 100],
                loading: false,
                showPageList: true,
                showRefresh: true,
                //刷新方法
                onBeforeRefresh: function (p, s) {
                    location.href = "${pageContext.request.contextPath }/admin/characters/list?cur_page=" + p + "&pageRow=" + s;
                },
                onSelectPage: function (p, s) {
                    location.href = "${pageContext.request.contextPath }/admin/characters/list?cur_page=" + p + "&pageRow=" + s;
                }
            });
        });

        function test(value, rec) {
            var url = "${pageContext.request.contextPath}/admin/characters/getById";
            $.post(
                url,
                {
                    "characterId": rec.characterId
                },
                function (data) {
                    $("#characterId").val(data.characterId);
                    $("#characterName").val(data.characterName);
                },
                "json"
            );
        }

        function del(id) {
            var url = "${pageContext.request.contextPath}/admin/characters/del";
            if (confirm('删除确认')) {
                $.post(
                    url,
                    {
                        "characterId": id
                    },
                    function (data) {
                        window.location.href = "${pageContext.request.contextPath }/admin/characters/list";
                    },
                    "json"
                );
            }
        }

        function save() {
            if($("#characterName").val()==""){
                alert('角色名称不能为空');
                return;
            }
            var url = "${pageContext.request.contextPath}/admin/characters/save";
            $.post(
                url,
                $("#characterForm").serialize()
                ,
                function (data) {
                    window.location.href = "${pageContext.request.contextPath }/admin/characters/list";
                },
                "json"
            );
        }

        function add() {
            $("#characterId").val();
            $("#characterName").val("");
        }

        function charactersAdd(id) {
            $("#charactersAdd").window(
                {
                    href: '${pageContext.request.contextPath}/admin/characters/charactersAdd?characterId=' + id,
                    onClose: function () {

                    }
                });
            $('#charactersAdd').window('refresh');
            $('#charactersAdd').window('open');
        }

        function charactersEmp(id) {
            $("#charactersEmp").window(
                {
                    href: '${pageContext.request.contextPath}/admin/characters/charactersEmp?characterId=' + id,
                    onClose: function () {

                    }
                });
            $('#charactersEmp').window('refresh');
            $('#charactersEmp').window('open');
        }

        function addModule(characterId) {
            var nodes = $("#module").tree("getChecked");
            var temp = undefined;
            var s = '';
            for (var i = 0; i < nodes.length; i++) {
                if (nodes[i].id != temp) {
                    if (s != '') s += ',';
                    s += nodes[i].id;
                }
            }
            $.post(
                "${pageContext.request.contextPath}/admin/characters/charactersSave",
                {
                    "ids": s,
                    "characterId": characterId
                }, function (data) {
                    $('#charactersAdd').window('refresh');
                }, "json"
            );
        }

        function addCharEmp(characterId) {
            var nodes = $("#charEmp").tree("getChecked");
            var temp = undefined;
            var s = '';
            for (var i = 0; i < nodes.length; i++) {
                if (nodes[i].id != temp) {
                    if (s != '') s += ',';
                    s += nodes[i].id;
                }
            }
            $.post(
                "${pageContext.request.contextPath}/admin/characters/charEmpSave",
                {
                    "ids": s,
                    "characterId": characterId
                }, function (data) {
                    $('#charactersEmp').window('refresh');
                }, "json"
            );
        }
    </script>
</head>
<body>
<table class="easyui-datagrid" pagination="true" style="height:340px" fitColumns="true" singleSelect="true"
       data-options="onClickRow:test">
    <thead>
    <tr>
        <th align="center" width="100" field="characterId">编号</th>
        <th align="center" width="200" field="characterName">角色名称</th>
        <th align="center" width="400" field="opt">操作</th>
    </tr>
    </thead>
    <tbody>
    <c:forEach items="${pager.datas}" var="d">
        <tr>
            <td>${d.characterId}</td>
            <td>${d.characterName}</td>
            <td>
                <a href="javascript:void(0);" class="easyui-linkbutton button-line-blue button-xs" onclick='charactersAdd(${d.characterId});'> 编辑权限</a>
                <a href="javascript:void(0);" class="easyui-linkbutton button-line-blue button-xs" onclick='charactersEmp(${d.characterId});'>用户授权</a>
                <a href="javascript:void(0);" class="easyui-linkbutton button-line-red button-xs" onclick='del(${d.characterId});'> 删除</a>

            </td>
        </tr>
    </c:forEach>
    </tbody>
</table>
<div id="tab-tools">
    <a href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'"
       onclick="add();">添加</a>
    <a href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-save'"
       onclick="save();">保存</a>
</div>
<div id="tt" class="easyui-tabs" style="height:220px;" data-options="tools:'#tab-tools'">
    <div title="角色设置" data-options="closable:false,cache:false" style="padding:0px;">
        <form method="post" id="characterForm" name="characterForm" style="margin-left: 150px;">
            <input type="hidden" id="characterId" name="characterId"/>
            <div style="margin-bottom:20px">
                <label class="label-top">角色名称&nbsp;&nbsp;:</label>
                <br/>
                <input class="easyui-textbox" data-options="required: true,missingMessage: '请输入角色名称'" id="characterName"
                       name="characterName" style="width:80%;">
            </div>
        </form>
    </div>
</div>
<div class="easyui-window" title="编辑权限" id="charactersAdd" modal="true" style="width: 600px; height: 380px;" mode="true"
     closed="true">
</div>
<div class="easyui-window" title="用户授权" id="charactersEmp" modal="true" style="width: 600px; height: 380px;" mode="true"
     closed="true">
</div>
</body>
</html>
